"""
mnist_loader
~~~~~~~~~~~~

A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``.  In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""

#### Libraries
# Standard library
#import cPickle
import pickle
import gzip

# Third-party libraries
import numpy as np
#import shareglob
from info.modules.api.neuralnet import shareglob
from info.modules.api.neuralnet.shareglob import *

def load_data():
    """Return the MNIST data as a tuple containing the training data,
    the validation data, and the test data.

    The ``training_data`` is returned as a tuple with two entries.
    The first entry contains the actual training images.  This is a
    numpy ndarray with 50,000 entries.  Each entry is, in turn, a
    numpy ndarray with 784 values, representing the 28 * 28 = 784
    pixels in a single MNIST image.

    The second entry in the ``training_data`` tuple is a numpy ndarray
    containing 50,000 entries.  Those entries are just the digit
    values (0...9) for the corresponding images contained in the first
    entry of the tuple.

    The ``validation_data`` and ``test_data`` are similar, except
    each contains only 10,000 images.

    This is a nice data format, but for use in neural networks it's
    helpful to modify the format of the ``training_data`` a little.
    That's done in the wrapper function ``load_data_wrapper()``, see
    below.
    """
    #f = gzip.open('../data/mnist.pkl.gz', 'rb')
    servern=shareglob.gservern
    print('servern in local= ',servern)#=shareglob.gservern
    if(servern==1):
       if(textilenum>=9. and textilenum<=11.):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-10.pkl.gz", 'rb')
       elif(textilenum>11. and textilenum<=15.):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-10-15.pkl.gz", 'rb')
       elif(textilenum>15. and textilenum<=19.5):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-15-20.pkl.gz", 'rb')
       elif(textilenum>19.5 and textilenum<=24.):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-20-24.pkl.gz", 'rb')
       elif(textilenum>24. and textilenum<=29.5):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-25-30.pkl.gz", 'rb')
       elif(textilenum>29.5 and textilenum<=35.):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-30-35.pkl.gz", 'rb')
       elif(textilenum>35. and textilenum<=40.):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-35-40.pkl.gz", 'rb')
       elif(textilenum>40. and textilenum<=59.5):
          f = gzip.open(r"info/modules/api/neuralnet/craft-out5-40.pkl.gz", 'rb')
    else:
      f = gzip.open(r"info/modules/api/neuralnet/craft-out5-10.pkl.gz", 'rb')
    training_data, validation_data, test_data = pickle.load(f, encoding="latin1")
    f.close()
    return (training_data, validation_data, test_data)

def load_data_wrapper():
    """Return a tuple containing ``(training_data, validation_data,
    test_data)``. Based on ``load_data``, but the format is more
    convenient for use in our implementation of neural networks.

    In particular, ``training_data`` is a list containing 50,000
    2-tuples ``(x, y)``.  ``x`` is a 784-dimensional numpy.ndarray
    containing the input image.  ``y`` is a 10-dimensional
    numpy.ndarray representing the unit vector corresponding to the
    correct digit for ``x``.

    ``validation_data`` and ``test_data`` are lists containing 10,000
    2-tuples ``(x, y)``.  In each case, ``x`` is a 784-dimensional
    numpy.ndarry containing the input image, and ``y`` is the
    corresponding classification, i.e., the digit values (integers)
    corresponding to ``x``.

    Obviously, this means we're using slightly different formats for
    the training data and the validation / test data.  These formats
    turn out to be the most convenient for use in our neural network
    code."""
    tr_d, va_d, te_d = load_data()
    nin=13
    nout=5
    #training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
    training_inputsori = tr_d[0]#[np.reshape(x, (1, 1)) for x in tr_d[0]]
    #training_inputs = [np.reshape(x, (nin, 1)) for x in tr_d[0]]
    training_inputs=[]
    for i in range(len(tr_d[0])):
       training_inputs.append([training_inputsori[i][0][j] for j in range(nin)])

    testt=np.array(training_inputs)
    print("len(train)=",testt.shape,len(testt),len(tr_d[0]))
    training_results =tr_d[1]#[np.reshape(x, (2,1)) for x in tr_d[1]]
#    training_resultsori =tr_d[1]#[np.reshape(x, (2,1)) for x in tr_d[1]]
#    training_results=[]
#    #training_results =[np.reshape(x, (nout,1)) for x in tr_d[1]]
#    testtt=np.array(training_resultsori)
#    print("len(train)=",testtt.shape,len(testtt),len(tr_d[1]))
#    for i in range(len(tr_d[0])):
#       training_results.append([training_resultsori[i][0][j] for j in range(nout)])

    testtto=np.array(training_results)
    print("len(train)=",testtto.shape,len(testtto),len(tr_d[1]))
    #training_data = [training_inputs,training_results]
    training_data = list(zip(training_inputs, training_results))
    print("y=",training_data[0])#,tr_d[1][0])
    #print("data format",training_data[0])# = zip(training_inputs, training_results)
    #validation_inputs = va_d[0]#[np.reshape(x, (nin, 1)) for x in va_d[0]]
    validation_inputs = [np.reshape(x, (nin, 1)) for x in va_d[0]]
    #validation_results = va_d[1]#[np.reshape(x, (2,1)) for x in va_d[1]]
    validation_results =[np.reshape(x, (nout,1)) for x in va_d[1]]

    validation_inputsori=va_d[0]
    validation_inputs=[]
    for i in range(len(va_d[0])):
        validation_inputs.append([validation_inputsori[i][0][j] for j in range(nin)])
##    validation_inputs = va_d[0]#[np.reshape(x, (nin, 1)) for x in va_d[0]]
    ##validation_inputs = [np.reshape(x, (nin, 1)) for x in va_d[0]]
    validation_results = va_d[1]#[np.reshape(x, (2,1)) for x in va_d[1]]
    ##validation_results =[np.reshape(x, (nout,1)) for x in va_d[1]]
    validation_data = list(zip(validation_inputs, validation_results))
#    test_inputs = te_d[0]#[np.reshape(x, (nin, 1)) for x in te_d[0]]
#    test_results = te_d[1]#[np.reshape(x, (2,1)) for x in te_d[1]]
    test_inputs = [np.reshape(x, (nin, 1)) for x in te_d[0]]
    test_results =[np.reshape(x, (nout,1)) for x in te_d[1]]
    test_data = list(zip(test_inputs, test_results))
    print('Ok for now in load.')
    return (training_data, validation_data, test_data)

def vectorized_result(j):
    """Return a 10-dimensional unit vector with a 1.0 in the jth
    position and zeroes elsewhere.  This is used to convert a digit
    (0...9) into a corresponding desired output from the neural
    network."""
    #e = np.zeros((10, 1))
    e = np.zeros((186, 1))
    #e[j] = 1.0
    e[j] = 1.0
    return e
